//
//  GuidePage.swift
//  Uber
//
//  Created by 朱 桓 on 16/1/6.
//  Copyright © 2016年 zhu.huan. All rights reserved.
//

import UIKit
import AVFoundation

class GuidePage: ZhBaseController {
    
    @IBOutlet var backImageView:UIImageView?
    @IBOutlet weak var backView: UIView!
    
    var player:AVPlayer!
    var playerItem:AVPlayerItem!
    var location:ZhLocation!
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?)
    {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    convenience init ()
    {
        var nibNameOrNil = String?("GuidePage")
        
        //考虑到xib文件可能不存在或被删，故加入判断
        if NSBundle.mainBundle().pathForResource(nibNameOrNil, ofType: "nib") == nil {
            nibNameOrNil = nil
        }
        
        self.init(nibName: nibNameOrNil, bundle: nil)
    }
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        // 视频显示
        initPlayVideo()
        // 动画显示
        doAnimation()
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }
    
    
    /**
    *动画显示，延迟5秒显示视频
    */
    func doAnimation()
    {
        var images:[UIImage] = []
        var imgName:String = "" // 图片名字
        var image:UIImage?
        
        for var index = 0; index <= 67; index++ {
            imgName = "logo-" + String(format: "%03d", index)
            
            image = UIImage(named: imgName)
            images.append(image!)
        }
        // 播放图片
        backImageView?.animationImages = images
        // 重复次数
        backImageView?.animationRepeatCount = 1
        // 设置播放时间
        backImageView?.animationDuration = 5
        backImageView?.startAnimating()
        
        // 延迟5秒显示视频
        UIView.animateWithDuration(0.7, delay: 5, options: UIViewAnimationOptions.CurveEaseOut, animations: { () -> Void in
            self.backView.alpha = 1.0
            self.player?.play()
            }) { (finish) -> Void in
                print("Animate finished")
        }
        
    }
    
    /**
    *播放视频
    */
    func initPlayVideo()
    {
        let path = NSBundle.mainBundle().pathForResource("welcome_video", ofType: "mp4")
        let url = NSURL.fileURLWithPath(path!)
        
        playerItem = AVPlayerItem(URL: url)
        player = AVPlayer(playerItem: playerItem)
        
        let playerLayer = AVPlayerLayer(player: player)
        
        playerLayer.frame = backView!.bounds
        playerLayer.videoGravity =  AVLayerVideoGravityResizeAspect
        
        backView!.layer.insertSublayer(playerLayer, atIndex: 0)
        backView!.alpha = 0.0
        
        // 视频结束后发送通知
        NSNotificationCenter.defaultCenter().addObserver ( self,
            selector: "didFinishVideo:" ,
            name: AVPlayerItemDidPlayToEndTimeNotification ,
            object: playerItem)

    }
    
    /**
    *循环播放视频
    */
    func didFinishVideo(sender: NSNotification)
    {
        let player = sender.object as! AVPlayerItem
        // 视频重置
        player.seekToTime(kCMTimeZero)
        
        self.player?.play()
    
    }
    
    /**
    *登录入口函数
    */
    @IBAction func loginBtnClicked(sender: UIButton)
    {
        // 开启定位服务
        location = ZhLocation()
        location.startLocation()
    }
    
    /**
    *注册入口函数
    */
    @IBAction func registerBtnClicked(sender: UIButton)
    {
        let createAccount = CreateAccountPage()
        let navPage = UINavigationController(rootViewController: createAccount)
        
        self.presentViewController(navPage, animated: true, completion: nil)
    }
}
